<?php
namespace app\console\controller;

use app\console\model\UserAuth;
use app\console\model\News;
use think\Cache;

class Index extends Base
{
    public function index()
    {
        $user_info = $this->user;
        return view('index', compact('user_info'));
    }

    public function cache() {  
        array_map('unlink', glob(TEMP_PATH . '/*.php'));

        $this->success( '清除缓存成功', 'index/index' );  
    }  

    public function welcome()
    {
        return view('welcome');
    }

    public function test()
    {
    	// echo '1111111';
    	return view('test');
    }

    //用户申请认证消息提醒
    public function checkauth()
    {
        $res = UserAuth::where('status', 3)->count();
        // dump(json_decode( json_encode( $res),true));exit;
        $msg = '有 '.$res. '个用户待审核认证！';
        $title = '用户认证提醒';
        if($res){
            return success($title, $msg);
        }
    }

    //文章审核消息提醒
    public function checknews()
    {
        $res = News::where('status', 2)->count();
        // dump(json_decode( json_encode( $res),true));exit;
        $msg = '目前有 '.$res. ' 篇文章等待审核！';
        $title = '文章审核消息';
        if($res){
            return success($title, $msg);
        }
    }

    /*
     *  wang编辑器图片上传方法
     */
    public function wang_upload()
    {
        //图片上传处理
        $files = request()->file();
        $imags = [];
        $errors = [];

        foreach($files as $file) {
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->validate(['ext' => 'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'wang');
            if ($info) {
                $path = "/uploads/wang/" . $info->getSaveName();
                array_push($imags,$path);
            } else {
                array_push($errors,$file->getError());
            }
        }

        if(!$errors){
            $msg['errno'] = 0;
            $msg['data'] = $imags;
            return json($msg);
        }else{
            $msg['errno'] = 1;
            $msg['data'] = $imags;
            $msg['msg'] = "上传出错";
            return json($msg);
        }
    }
}
